package com.hujf.netty.nio.zerocopy;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * @author Hujf
 * @title: NewIOClient
 * @date 2021/1/26 0026下午 4:51
 * @description: TODO
 */
public class NewIOClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("localhost",7001));
        String file = "F:\\百度云下载\\47821，郑人杰 软件工程概论（第2版）.zip";
        //得到文件的通道
        FileChannel fileChannel =new FileInputStream(file).getChannel();
        long start = System.currentTimeMillis();

        //linux 一个transferto 一个文件
        //windows 一个8M 需要分段 注意传输位置
        //treansferto 零拷贝
        long transcount =fileChannel.transferTo(0,fileChannel.size(),socketChannel);
        System.out.println("发送字节"+transcount+" 传输时间"+(System.currentTimeMillis()-start));

        fileChannel.close();
    }
}
